package com.example.lib_cache.network;

import android.app.Application;
import android.util.Log;

import androidx.lifecycle.ViewModel;

import com.example.lib_cache.service.ApiService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.components.ApplicationComponent;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

@InstallIn(ApplicationComponent.class)
@Module
public class NetWorkModule {

    public final static String TAG = NetWorkModule.class.getSimpleName();

    @Singleton
    @Provides
    OkHttpClient provideOkHttpClient(){

        HttpCommonInterceptor httpCommonInterceptor = new HttpCommonInterceptor();


//        HttpLoggingInterceptor interceptor =
//                new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
//            @Override
//            public void log(String message) {
//                Log.d(TAG,message);
//            }
//
//        }).setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder()
                .addInterceptor(httpCommonInterceptor)
                .build();
    }

    @Singleton
    @Provides
    Retrofit provideRetrofit(OkHttpClient client){
        return new Retrofit.Builder()
                .baseUrl("http://mycarcalling.cpolar.cn/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(new Gson().newBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @Singleton
    @Provides
    ApiService provideApiService(Retrofit retrofit){
        return retrofit.create(ApiService.class);
    }
}
